import Vue from 'vue'
import Vuex from 'vuex'
import * as db from '../util/db.js'
Vue.use(Vuex)
let userInfo = db.get('userInfo') || {}
let token = db.get('token') || ''

const store = new Vuex.Store({
	state: {
		userInfo: userInfo,
		token: token,
		orderInfo:{}
	},
	mutations: {
		saveUserTokenAction(state, token) {
			state.token = token;
			db.set('token', state.token)
		},
		setUserInfoMutations(state, userinfo) {
			state.userInfo = userinfo;
			db.set('userInfo', state.userInfo)
		},
		saveOrderInfoMutations(state,orderInfo){
			state.orderInfo = orderInfo;
			db.set('orderInfo', state.orderInfo)
		}
	},
	actions: {
		saveUserTokenAction(state, token) {
			state.commit('saveUserTokenAction', token)
		},
		setUserInfoAction(state, userinfo) {
			state.commit('setUserInfoMutations', userinfo)
		},
		saveOrderInfoAction(state, orderInfo){
			state.commit('saveOrderInfoMutations', orderInfo)
		}
	},
	getters: {
		getUserInfos(state) {
			return state.userInfo;
		},
		getisLogin(state) {
			if (JSON.stringify(state.userInfo) == "{}") {
				return false;
			} else {
				return true;
			}
		},
		getOrderInfo(state){
			return state.orderInfo;
		}
	}
})

export default store
